﻿using AutoFlatnessTest.Db;
using AutoFlatnessTest.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoFlatnessTest.Plc
{
    public static class InovanceRun
    {
        public static InovanceModbusTCPTool Plc;

        public static void Open()
        {
            Plc = new InovanceModbusTCPTool(ConModel.Data.PlcLoca.Ip, ConModel.Data.PlcLoca.Port, 1);
            Plc.Connect();
        }

        public static bool IsConnect()
        {
            if (Plc == null)
                return false;

            return Plc.Ping();
        }

        public static void Close()
        {
            Plc.CloseConnect();
        }

        /// <summary>
        /// 装载数据
        /// </summary>
        /// <param name="proj"></param>
        /// <param name="isNorm"></param>
        public static void LoadData(Project proj, MoShi MoShi)
        {
            InovanceRun.Plc.SetData<Int16>(InovanceComm.行数, Convert.ToInt16(proj.RowNum));
            InovanceRun.Plc.SetData<Int16>(InovanceComm.列数, Convert.ToInt16(proj.ColumnNum));

            InovanceRun.Plc.SetData<Int32>(InovanceComm.单板移动距离, Convert.ToInt32(proj.OneWidth));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.单板扫描仪伺服起始位置, Convert.ToInt32(proj.OneSmi1));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.单板扫描仪伺服结束位置, Convert.ToInt32(proj.OneSmi2));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.单板扫码枪伺服起始位置, Convert.ToInt32(proj.OneSma1));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.单板扫码枪伺服结束位置, Convert.ToInt32(proj.OneSma2));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.单板运载伺服起始位置, Convert.ToInt32(proj.OneYz1));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.单板运载伺服结束位置, Convert.ToInt32(proj.OneYz2));

            //InovanceRun.Plc.SetData<Int32>(InovanceComm.连续扫描仪伺服起始位置, Convert.ToInt32(proj.TwoSmi1));
            //InovanceRun.Plc.SetData<Int32>(InovanceComm.连续扫描仪伺服结束位置, Convert.ToInt32(proj.TwoSmi2));
            //InovanceRun.Plc.SetData<Int32>(InovanceComm.连续扫码枪伺服起始位置, Convert.ToInt32(proj.TwoSma1));
            //InovanceRun.Plc.SetData<Int32>(InovanceComm.连续扫码枪伺服结束位置, Convert.ToInt32(proj.TwoSma2));
            //InovanceRun.Plc.SetData<Int32>(InovanceComm.连续运载伺服起始位置, Convert.ToInt32(proj.TwoYz1));
            //InovanceRun.Plc.SetData<Int32>(InovanceComm.连续运载伺服结束位置, Convert.ToInt32(proj.TwoYz2));

            if (MoShi == MoShi.录入 || MoShi == MoShi.抽测)
                InovanceRun.Plc.SetData<Int16>(InovanceComm.方式, Convert.ToInt16((int)MoShi.标准));
            else
                InovanceRun.Plc.SetData<Int16>(InovanceComm.方式, Convert.ToInt16((int)MoShi));
        }

        /// <summary>
        /// 装载数据
        /// </summary>
        /// <param name="proj"></param>
        /// <param name="isNorm"></param>
        public static void LoadData(DotData dot)
        {
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准移动距离, Convert.ToInt32(dot.Ydjl));

            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准1号运载伺服位置, Convert.ToInt32(dot.Yz1));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准1号扫描仪伺服位置, Convert.ToInt32(dot.Smi1));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准2号运载伺服位置, Convert.ToInt32(dot.Yz2));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准2号扫描仪伺服位置, Convert.ToInt32(dot.Smi2));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准3号运载伺服位置, Convert.ToInt32(dot.Yz3));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准3号扫描仪伺服位置, Convert.ToInt32(dot.Smi3));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准4号运载伺服位置, Convert.ToInt32(dot.Yz4));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准4号扫描仪伺服位置, Convert.ToInt32(dot.Smi4));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准5号运载伺服位置, Convert.ToInt32(dot.Yz5));
            InovanceRun.Plc.SetData<Int32>(InovanceComm.校准5号扫描仪伺服位置, Convert.ToInt32(dot.Smi5));

            InovanceRun.Plc.SetData<Int16>(InovanceComm.方式, 3);
        }

        /// <summary>
        /// 得到设备状态
        /// </summary>
        /// <returns>-1 链接失败 1运行 2初始化 3停止</returns>
        public static int GetState()
        {
            try
            {
                var data = InovanceRun.Plc.GetData<Int16>(InovanceComm.设备停止状态);
                if (data == 2)
                    return 3;

                data = InovanceRun.Plc.GetData<Int16>(InovanceComm.设备初始化完成状态);
                if (data == 2)
                    return 2;

                data = InovanceRun.Plc.GetData<Int16>(InovanceComm.设备运行状态);
                if (data == 2)
                    return 1;
            }
            catch (Exception)
            {
                return -1;
            }

            return -1;
        }

    }


}
